import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';

import { LoggerMiddleware } from './common/middleware/logger.middleware';

import { HelloModule } from './modules/hello/hello.module';
import { ExceptionModule } from './modules/exception/exception.module';
import { ExcepitonController } from './modules/exception/exception.controller';
import { HelloController } from './modules/hello/hello.controller';
import { PipeModule } from './modules/usePipe/pipe.module';
import { GuardModule } from './modules/guard/guard.module';

@Module({
  imports: [HelloModule, ExceptionModule, PipeModule, GuardModule],
})

export class AppModule {
  // 中间件
  configure(consumer: MiddlewareConsumer) {
    // 给hello路由添加中间件
    consumer
      .apply(LoggerMiddleware) // 添加中间件
      .exclude({path: 'hello', method: RequestMethod.POST}, {path: 'exception', method: RequestMethod.GET}) // 忽略hello的post请求 需要中间件处理的中间件的单个请求方式不需要中间件处理
      .forRoutes( HelloController ) // 根目录, 写入需要进行中间件处理的模块
  }
}
